/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minValue;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author mani
 */
public class SaveMoneyXML {

    private Document read1;
    private ArrayList<Element> aeAl;
    private Element rootElement1;
    private String stfilelist;

    public SaveMoneyXML() throws DocumentException, IOException {
        stfilelist = "";
        aeAl = new ArrayList<Element>();
        SAXReader sr2 = new SAXReader();
        try {
            read1 = sr2.read("savemoney.xml");
        } catch (Exception e) {
            Document document = DocumentHelper.createDocument();
            Element booksElement = document.addElement("money ");

            XMLWriter writer = new XMLWriter(new FileWriter(new File("savemoney.xml")));
            writer.write(document);
            writer.close();
            read1 = sr2.read(new File("savemoney.xml"));
        }
        rootElement1 = read1.getRootElement();
    }

    void SortLast() throws Exception {
        double minerror = Double.MAX_VALUE;
        int correct = 0;
        int begcorrect = 0;
        Element minNode = null;
        if (aeAl.isEmpty()) {
            return;
        }
        String[] split = new String[2];
        for (Element element : aeAl) {
            split[0] = element.attribute("false").getValue();
            split[1] = element.attribute("true").getValue();
            double[] d1 = null;
            double[] d2 = null;
            try {
                d1 = getDoubleArray(split[0].split(":"));
                d2 = getDoubleArray(split[1].split(":"));
            } catch (Exception e) {
                continue;
            }
            if (d1.length != d2.length) {

                continue;
            }
            // double rms = RootMeanSqure(d1, d2);
            Aera aa = new Aera(d1, d2);
            arearesult rms = aa.computeArea();
            if (rms.getSum() < minerror) {
                minerror = rms.getSum();
                correct = rms.getCorrect();
                begcorrect = aa.computeBegCorrect();
                minNode = element;
            }
        }
        if (minNode == null && correct == 0) {
            return;
        }
        SaveMinError(minNode, correct, begcorrect);
    }

    void SortLast(boolean t) throws Exception {
        double minerror = Double.MAX_VALUE;
        int correct = 0;
        int begcorrect = 0;
        Element minNode = null;
        if (aeAl.isEmpty()) {
            return;
        }
        String[] split = new String[2];
        for (Element item : aeAl) {
            split[0] = item.attribute("false").getValue();
            split[1] = item.attribute("true").getValue();
            double[] d1 = null;
            double[] d2 = null;
            try {
                d1 = getDoubleArray(split[0].split(":"));
                d2 = getDoubleArray(split[1].split(":"));
            } catch (Exception e) {
                continue;
            }
            if (d1.length != d2.length) {

                continue;
            }
            // double rms = RootMeanSqure(d1, d2);
            double error = Double.parseDouble(item.attribute("error").getValue());
      
            Aera aa = new Aera(d1, d2);
            arearesult rms = aa.computeArea();
            if (error < minerror) {
                minerror =  error;
                correct = rms.getCorrect();
                begcorrect = aa.computeBegCorrect();
                minNode = item;
            }
        }
        if (minNode == null && correct == 0) {
            return;
        }
        SaveMinError(minNode, correct, begcorrect);
    }

    void ClearNode() {
        if (aeAl.size() > 0) {
            aeAl.clear();
        }
    }

    void AddElement(Element object) {
        boolean add = aeAl.add(object);
    }

    private void SaveMinError(Element minNode, int correct, int begcorrect) {
        Element addElement = rootElement1.addElement("network");


        if (stfilelist.isEmpty()) {
            stfilelist = minNode.attribute("egfile").getValue();
        } else {
            stfilelist = stfilelist + ":" + minNode.attribute("egfile").getValue();
        }
        S2D s2d = new S2D("");
        String yahoo = minNode.attribute("yahoo").getValue();
        addElement.addAttribute("yahoo", yahoo.substring(2));
        addElement.addAttribute("egfile", minNode.attribute("egfile").getValue());
        addElement.addAttribute("BasicTrain", minNode.attribute("BasicTrain").getValue());
        addElement.addAttribute("Logic", minNode.attribute("Logic").getValue());
        addElement.addAttribute("date", "2011-1-19");
        addElement.addAttribute("false", "");
        addElement.addAttribute("true", "true");
        addElement.addAttribute("f1", minNode.attribute("false").getValue());
        addElement.addAttribute("f2", minNode.attribute("true").getValue());

        addElement.addAttribute("correct", Integer.toString(correct));
        addElement.addAttribute("begcorrect", Integer.toString(begcorrect));
    }

    void Save() throws IOException {
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter write = new XMLWriter(new FileWriter("savemoney.xml"), format);
        write.write(read1);
        write.close();
    }

    private static double[] getDoubleArray(String[] split1) {
        double[] gda = new double[split1.length];
        for (int i = 0; i < split1.length; i++) {
            gda[i] = Double.parseDouble(split1[i]);
        }

        return gda;

    }

    void DeleteFile(String string) {

        File fe = new File(string + "network");
        if (!fe.exists()) {
            return;
        }
        String[] filelist = fe.list();


        for (String file : filelist) {


            if (stfilelist.indexOf(file) == -1) {

                File fee = new File(string + "network" + File.separatorChar + file);
                boolean delete = fee.delete();
            }
        }
    }

    void ClearSavemoney() {
        Iterator it = rootElement1.elementIterator();
        ArrayList<Element> asn = new ArrayList<Element>();
        while (it.hasNext()) {
            Element next = (Element) it.next();
            asn.add(next);
        }
        for (Element node : asn) {
            rootElement1.remove(node);
        }
    }

    private double RootMeanSqure(double[] falsed, double[] trued) {
        double tsum = 0;
        for (int i = 0; i < trued.length; i++) {
            tsum = tsum + trued[i];
        }
        tsum = tsum / trued.length;
        double sum = 0;
        for (int i = 0; i < falsed.length; i++) {
            sum = sum + (falsed[i] - tsum) * (falsed[i] - tsum);
        }
        return Math.sqrt(sum);
    }
}
